﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Restaurant.DAO
{
    public class DatChoDB
    {
        private SqlConnection con = KetNoiDB.TaoKetNoi();

        public DataTable LoadDatCho()
        {
            con.Open();
            string strcmd = "dbo.spLoadDatCho";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public bool ThemDatCho(int maKH, DateTime ngayDat, DateTime ngayDen, int tinhTrang, int soLuong, int maNH)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spThemDatCho";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKH", maKH));
                cmd.Parameters.Add(new SqlParameter("@ngayDat", ngayDat));
                cmd.Parameters.Add(new SqlParameter("@ngayDen", ngayDen));
                cmd.Parameters.Add(new SqlParameter("@tinhTrang", tinhTrang));
                cmd.Parameters.Add(new SqlParameter("@soLuong", soLuong));
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaDatCho(int maDatCho,int maKH,int maban)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spXoaDatCho";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maDatCho", maDatCho));
                cmd.Parameters.Add(new SqlParameter("@maKH", maDatCho));
                cmd.Parameters.Add(new SqlParameter("@maBan", maban));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaDatCho(int maDatCho, int maKH, DateTime ngayDat, DateTime ngayDen, int tinhTrang, int soLuong, int maNH)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spSuaDatCho";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maDatCho", maDatCho));
                cmd.Parameters.Add(new SqlParameter("@maKH", maKH));
                cmd.Parameters.Add(new SqlParameter("@ngayDat", ngayDat));
                cmd.Parameters.Add(new SqlParameter("@ngayDen", ngayDen));
                cmd.Parameters.Add(new SqlParameter("@tinhTrang", tinhTrang));
                cmd.Parameters.Add(new SqlParameter("@soLuong", soLuong));
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public DataTable TimDatChoID(int ma)
        {
            con.Open();
            string strcmd = "dbo.spTimDatChoID";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maDatCho", ma));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimDatChoMaKH(int makh)
        {
            con.Open();
            string strcmd = "dbo.spTimDatChoKhachHang";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ma", makh));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimDatChoNgay(DateTime ngayDat, DateTime ngayDen)
        {
            con.Open();
            string strcmd = "dbo.spTimDatChoNgay";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ngayDat", ngayDat));
            cmd.Parameters.Add(new SqlParameter("@ngayDen", ngayDen));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimDatChoMaNH(int manh)
        {
            con.Open();
            string strcmd = "dbo.spTimDatChoNhaHang";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ma", manh));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimDatChoSoLuong(int soluong)
        {
            con.Open();
            string strcmd = "dbo.spTimDatChoSoLuong";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soLuong", soluong));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimDatChoTinhTrang(string tinhTrang)
        {
            con.Open();
            string strcmd = "dbo.spTimDatChoTinhTrang";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@tinhTrang", tinhTrang));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public bool ThemCTDatCho(int maDatCho, int maBan)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spThemCTDatCho";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maDatCho", maDatCho));
                cmd.Parameters.Add(new SqlParameter("@maBan", maBan));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable LoadDatChoPhantomError(ref int soluong)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spPhantom_TimDatCho_Error";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
                cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
                con.Close();
                return dbtb;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LoadDatChoPhantom(ref int soluong)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spPhantom_TimDatCho_Fixed";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
                cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
                con.Close();
                return dbtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LoadDatChoUnrepeatError(ref int soluong)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spUnrepeatable_TimDatCho_Error";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
                cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
                con.Close();
                return dbtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LoadDatChoUnrepeat(ref int soluong)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spUnrepeatable_TimDatCho_Fixed";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
                cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
                con.Close();
                return dbtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
